export const ADD_USER_BROW_HISTORY = "ADD_USER_BROW_HISTORY";
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS'

interface AddUserBrowHistoryType {
  type: typeof ADD_USER_BROW_HISTORY;
  payload: string;
}

export function addUserBrowHistoryActionCreator(
  payload: string
): AddUserBrowHistoryType {
  return { type: ADD_USER_BROW_HISTORY, payload };
}


interface LoginSuccessType {
  type: typeof LOGIN_SUCCESS;
  payload: { username: string, token: string };
}

export function loginSuccessActionCreator(
  payload: { username: string, token: string }
): LoginSuccessType {
  return { type: LOGIN_SUCCESS, payload };
}




type UserAction = AddUserBrowHistoryType | LoginSuccessType

const userReducer = (state = initState, action: UserAction) => {
  switch (action.type) {
    case ADD_USER_BROW_HISTORY: {
      return {
        ...state,
        userBrowhistory: [action.payload, ...state.userBrowhistory],
      };
    }
		case LOGIN_SUCCESS: {
			return {
				...state,
				isLogin: true,
				token: action.payload.token,
				username: action.payload.username
			}
		}
    default:
      return state;
  }
};

interface UserState {
	username: string,
  token: string,
  isLogin: boolean,
  userBrowhistory: string[],
}

const initState: UserState = {
  username: "",
  token: "",
  isLogin: false,
  userBrowhistory: [],
};

export default userReducer;
